<?php

namespace app\personal\controller;

use library\Controller;
use app\common\service\CompressService;
use app\admin\service\NodeService;
use library\File;
use think\Db;

/**
 * 我的
 * Class User
 * @package app\personal\controller
 */
class User extends Base
{
    /**
     * 首页
     */
    public function index()
    {
	  	$member = $this->member; 
	  	$person = Db::name('system_person')->where(['mid'=>$member['id']])->value('id');
	  	if($person){
	  		//获取补缴账单待付款数量
	  		$this->makeup = Db::name('system_makeup_bill')->where(['person_id'=>$person,'is_deleted'=>0,'status'=>2])->count();
	  		//获取增值账单待付款数量
	  		$this->appreciation = Db::name('system_appreciation_bill')->where(['person_id'=>$person,'is_deleted'=>0,'status'=>2])->count();
	  	}	  		
		$this->fetch();		   
    }
    
    
    
    /**
     * 社保服务
     */
    public function services()
    {
	  	$member = $this->member; 
	  	$where[] = ['mid','eq',$member['id']];
		$where[] = ['is_deleted','eq',0];
		$where[] = ['type','eq',2];
		$status = $this->request->get('status');
        if($status){
        	$where[] = ['status','=',$status];
        }
		$list = Db::name('system_service_handle')->where($where)
				->field('id,code,status,company_id,service_id,create_at')
    			->order('id desc')
    			->limit(10)
    			->select();
    	if($list){
    		foreach($list as $k=>$v){
    			$company_name = Db::name('system_company')->where(['id'=>$v['company_id']])->value('name');
    			$service_title = Db::name('system_service')->where(['id'=>$v['service_id']])->value('title');
    			$list[$k]['company_name'] = $company_name;
    			$list[$k]['service_title'] = $service_title;
    			$list[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
    		}
    	}
		$this ->list = $list;	
		$this->fetch();		   
    }
    
    
    
    /**
     * 账单列表ajax
     */
    public function page_list()
    {
    	$page = $this->request->post('page');
    	if(!is_numeric($page)){
			$this->error('参数错误');
		}
		$minpage = ($page-1)*10;
		$member = $this->member; 
		$where[] = ['mid','eq',$member['id']];
		$where[] = ['is_deleted','eq',0];
		$where[] = ['type','eq',2];
		$status = $this->request->post('status');
        if ($status){
        	$where[] = ['status','=',$status];            
        }
		$list = Db::name('system_service_handle')->where($where)
				->field('id,code,status,company_id,service_id,create_at')
    			->order('id desc')
    			->limit($minpage,10)
    			->select();
    	if($list){
    		foreach($list as $k=>$v){
    			$company_name = Db::name('system_company')->where(['id'=>$v['company_id']])->value('name');
    			$service_title = Db::name('system_service')->where(['id'=>$v['service_id']])->value('title');
    			$list[$k]['company_name'] = $company_name;
    			$list[$k]['service_title'] = $service_title;
    			$list[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
    		}
    	}
		$this ->list = $list; 
        $this->success('获取成功',$list);	
		
    }
    
    
    
    
    /**
     * 更改密码
     */
    public function changepw()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
			trim_array($data);
			$member = Db::name('system_member')->where(['id'=>$this->member['id']])->find();
			if($member['password'] != md5($data['orpassword'])){
				$this->error('原密码错误');
			}
			if($data['password'] != $data['repassword']){
				$this->error('两次密码不一致');
			}
			$result = NodeService::checkpwd($data['password']);
            if (empty($result['code'])) $this->error($result['msg']);
			$re = Db::name('system_member')->where(['id'=>$this->member['id']])->update(['password'=>md5($data['password'])]);
			if($re){
				\think\facade\Session::clear();
        		\think\facade\Session::destroy();
				$this->success('密码修改成功，请重新登录', url('personal/login/index'));				
			}else{
				$this->error('修改失败，请稍后再试');
			}
    	}
        $this->fetch();
    }
    
    
    /**
     * 关于我们
     */
    public function aboutus()
    {
    	
    	
    	$this->fetch();	
    }
    
    
    
    
    
    
    
    
    
    
    
}
